let app = getApp()
var stylecss = require('../utils/stylecss.js');
Component({
  properties: {
    scrollTop: {
      type: Number,
      observer: function(newVal, oldVal) {
        this.updatscroll()
      }
    },
    if_diy:{type:Number},
    styleType: {
      type: String
    },
    item: {
      type: Object
    },
    list: {
      type: Array
    }
  },
      data:{ 
        moneySign:'¥'
      },
    lifetimes: {
        attached: function () {
          if(this.data.item!=null){
           var anima=stylecss.animated(this.data.item.id);
           this.setData({
               oldanima:anima,
               anima:anima
           })
          }
       }
      },
      ready() {
        let that = this;
        if(this.data.item!=null){
          const query = wx.createSelectorQuery().in(this)
          setTimeout(() => { 
             query.select('#'+this.data.item.id).boundingClientRect(res=>{
                  that.setData({
                    top:res.top
                  })
                }).exec();
           }, 0);
        }
        if(typeof(app.globalData.module_coupon)!='undefined')
        {
          this.setData({
            item:app.globalData.module_coupon,
            pagestyle:app.globalData.styles[app.globalData.module_coupon.id],
            group_item:app.globalData.styles[app.globalData.module_coupon.id+' .group_item'],
          })
       }
       if(this.data.item!=null){
        var css=stylecss.css(this.data.item.id);
        var id2=this.data.item.id+' .coupon-bg-hook';
        var group_item_style=stylecss.css(id2);
        this.setData({
          css:css,
          group_item_style:group_item_style
        })
       }
    },
    methods: {
      updatscroll:function(){
        var nowtop=this.data.scrollTop-this.data.top
        var oldanima=this.data.oldanima
        if(nowtop<=0){
         this.setData({
           anima:oldanima
         })
        }else{
          this.setData({
              anima:''
          })
        }
     },
      data_list:function(){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Shop_getCouponList',
            user_id:wx.getStorageSync('user_id'),
            page:1
          },
          success: function(res) {
             that.setData({
                 list:res.data.data
             })
          }
        })
        
      },
      coupondetail:function(){
        if(this.data.if_diy==1){
          wx.navigateTo({
            url: '/yb_shopv2/native/all/index?type=page_coupon_list&styleType=style2'
          })
        }
      },
      couponlinqu:function(e){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Shop_userGetCoupon',
            user_id:wx.getStorageSync('user_id'),
            coupon_id:e.currentTarget.dataset.id
          },
          success: function(res) {
            if(res.data.code==0){
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 1500
              })
              that.data_list();
            }else{
              wx.showToast({
                title:'领取成功',
                icon: 'success',
                duration: 1500
              })
            }
          }
        })
      }
    }
});
